"use strict";

const stripTrailingSlash = (value = "") => value.replace(/\/+$/, "");

const runtimeWindow = typeof window !== "undefined" ? window : undefined;

export const API_BASE_URL = stripTrailingSlash(
  (import.meta.env && import.meta.env.VITE_API_BASE_URL) ??
    (runtimeWindow && runtimeWindow.__API_BASE_URL__) ??
    "http://127.0.0.1:8000"
);

export const VIP_PAY_BASE_URL = stripTrailingSlash(
  (import.meta.env && import.meta.env.VITE_VIP_PAY_BASE_URL) ??
    (runtimeWindow && runtimeWindow.__VIP_PAY_BASE_URL__) ??
    `${API_BASE_URL}/vip`
);

export const EVENT_STREAM_BASE_URL = stripTrailingSlash(
  (import.meta.env && import.meta.env.VITE_EVENT_STREAM_BASE_URL) ??
    (runtimeWindow && runtimeWindow.__EVENT_STREAM_BASE_URL__) ??
    API_BASE_URL
);

export const withApiBase = (path = "") => {
  if (!path) {
    return API_BASE_URL;
  }
  return `${API_BASE_URL}${path.startsWith("/") ? path : `/${path}`}`;
};


